package com.genius.service.impl;

import com.genius.enums.ResponseType;
import com.genius.exception.GraceException;
import com.genius.mapper.NoticeMapper;
import com.genius.mapper.NoticeUserMapper;
import com.genius.service.NoticeService;
import com.genius.system.pojo.Notice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private NoticeUserMapper noticeUserMapper;

    @Override
    public Notice selectNoticeById(Long noticeId) {
        return noticeMapper.selectNoticeById(noticeId);
    }

    @Override
    public List<Notice> selectNoticeByPage(Notice notice) {
        return noticeMapper.selectNoticeByPage(notice);
    }

    @Override
    public List<Notice> selectNoticeByUserId(Long userId) {
        return noticeMapper.selectNoticeByUserId(userId);
    }

    @Override
    @Transactional
    public int insertNotice(Notice notice) {
        return noticeMapper.insertNotice(notice);
    }

    @Override
    @Transactional
    public int updateNotice(Notice notice) {
        return noticeMapper.updateNotice(notice);
    }

    @Override
    @Transactional
    public int deleteNoticeByIds(Long[] ids) {
        if(noticeUserMapper.selectNoticeUserIsExistByNoticeId(ids)){
            GraceException.display(ResponseType.DELETE_NOTICE_ERROR);
        }
        return noticeMapper.deleteNoticeByIds(ids);
    }
}
